<?php

/**
 * Yii Controller FrontController
 *
 * @author nguyenmanhluu
 * @since Sep 21, 2013 - 9:29:16 PM
 */
class FrontController extends CController
{

    public $token_const             = 'qymzsTIXAEyU';
    public $layout                  = '';
    public $menu                    = array();
    public $assetDir; //Khoi tao bien assetDir
    public $breadcrumbs             = array();
    public $customer                = NULL;
    public $biz                     = NULL;
    public $business_model          = NULL;
    public $currentLimit            = 8;
    public $user                    = NULL;
    public $facebook_meta_desc      = '';
    public $facebook_meta_title     = '';
    public $facebook_meta_image     = '';
    public $facebook_meta_site_name = '';
    public $facebook_meta_url       = '';
    public $meta_description        = '';
    public $meta_keywords           = '';

    function init()
    {
        Yii::app()->user->setStateKeyPrefix('_cus');
        parent::init();

        //Language implement
        // If there is a post-request, redirect the application to the provided url of the selected language
        if (isset($_POST['language']))
        {
            $lang               = $_POST['language'];
            $MultilangReturnUrl = $_POST[$lang];
            $this->redirect($MultilangReturnUrl);
        }
        // Set the application language if provided by GET, session or cookie
        if (isset($_GET['language']))
        {
            Yii::app()->language = $_GET['language'];
            Yii::app()->user->setState('language', $_GET['language']);
            $this->createCookie('language', $_GET['language']);
        }
        else if (Yii::app()->user->hasState('language'))
        {
            Yii::app()->language = Yii::app()->user->getState('language');
        }
        else if (isset(Yii::app()->request->cookies['language']))
        {
            Yii::app()->language = Yii::app()->request->cookies['language']->value;
        }


        Yii::app()->theme = 'frontend';
        $this->assetDir   = Yii::app()->theme->baseUrl . '/assets/';

        //$this->customer = Customer::model()->find();
        if (Yii::app()->user->isGuest == FALSE)
        {
            $this->user = Users::model()->findByPk(Yii::app()->user->id);
        }

        if (isset($_GET['biz']))
        {
            $this->business_model = Business::model()->find('biz_url=:u', array(
                ':u' => $_GET['biz']
            ));

            if ($this->business_model == NULL)
            {
//                throw new CHttpException(404, 'Not found');
                Yii::app()->request->redirect('frontend/site/error');
            }
            else
            {
                $this->facebook_meta_desc  = $this->business_model->facebook_meta_desc;
                $this->facebook_meta_url   = Yii::app()->getBaseUrl(true) . '/' . $this->business_model->biz_url;
                if ($this->business_model->facebook_meta_image != NULL)
                    $this->facebook_meta_image = Yii::app()->getBaseUrl(true) . '/' . $this->business_model->facebook_meta_image;
                //Business table
                if ($this->business_model->user)
                {
                    $this->facebook_meta_title = $this->business_model->user->business_name;
                    $this->biz                 = $this->business_model->user;
                }
                else
                {
                    throw new CHttpException(404, 'Not found');
                }
            }
        }

        //booking background
        if (isset($_GET['service']))
        {
            $service = Service::getRecordbySid($_GET['service']);
            if ($service)
            {
                $this->business_model = $service->user->business;
            }
            else
            {
                throw new CHttpException(404, 'Not found');
            }
        }

        if (isset($_GET['class']))
        {
            $class = Appoint::getRecordByID($_GET['class']);
            if ($class)
            {
                $this->business_model = $class->service->user->business;
            }
            else
            {
                throw new CHttpException(404, 'Not found');
            }
        }
    }

    /**
     * Attach behaviors to current controller
     * @see CController::behaviors()
     *
     * @return array
     * @since  1.9.0
     */
    public function behaviors()
    {
        return array(
            /**
             * Attach Rendering behaviors to utility methods (usefull with ajax calls)
             */
            'sweelixRendering' => array(
                'class' => 'ext.sweekit.behaviors.SwRenderBehavior',
            ),
        );
    }

    /**
     * For ajax upload
     * @return array
     */
    public function actions()
    {
        return array(
            'asyncUpload' => 'ext.sweekit.actions.SwUploadAction',
            'asyncDelete' => 'ext.sweekit.actions.SwDeleteAction',
        );
    }

    protected function autoFBlogin($email)
    {
        $identity = new UserIdentity($email, '');
        $identity->authenticateFB();
        Yii::app()->user->login($identity);

        $this->redirect(Yii::app()->createUrl('frontend/site'));
    }

    public function getToken($data = array())
    {
        return md5(implode("+", $data) . $this->token_const);
    }

    public function createMultilanguageReturnUrl($lang = 'nl')
    {
        if (count($_GET) > 0)
        {
            $arr             = $_GET;
            $arr['language'] = $lang;
        }
        else
        {
            $arr = array('language' => $lang);
        }

        return $this->createUrl('', $arr);
    }

    /*
     * Creates a popularity cookie with a specific duration set in Yii params.
     */

    public function createCookie($name, $value)
    {
        $cookie                             = new CHttpCookie($name, $value);
        $cookie->expire                     = time() + Yii::app()->params['cookieDuration'];
        Yii::app()->request->cookies[$name] = $cookie;
    }

}

/**
 * End file FrontController
 */
